<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;
use Home\Model\User;
//测试控制器
class RegisterController extends Controller{
	private $data;                                   //设置一个属性构造函数，方便下面多次调用
	public function __init(){
		$this->data= new User;
	}
    //动作
    public function index(){
    	if(IS_POST){
    		//获取注册的用户名和密码
    		$username = Q('post.username');
			$password = Q('post.password');
			$cpassword = Q('post.confirmPassword');
			//对比两次密码，不一样就返回
			if($password != $cpassword){
				echo json_encode(array('status'=>0,'message'=>'两次密码不一样'));
				exit;
			}
			// sp(Q('post.'));die;
			//必须勾选服务协议
			if(!Q('post.agree')){
				View::success('请勾选服务协议');
				exit;
			}
			//调用模型方法，验证
			$model = $this->data;
			if($model->store($username,$password)){
				go(U('Index/index'));
			}
			$this->error($model->getError());
    	}
        View::make();
    }
	//AJAX验证数据库中是否有相同的用户名
	public function check(){
		$name = Q('post.username');
		$username = $this->data->where('username','=',$name)->get();
		if($username){
			$this->ajax(array('status'=>0,'message'=>'用户名已存在'));
		}else{
			$this->ajax(array('status'=>1,'message'=>'可以注册'));
		}
	}
	//调用框架验证码
	public function code(){
		Code::make();
	}
	//验证码验证
	public function codeCheck(){
		$code = Q('post.code');
		$code = strtolower($code);
		$session= Session::get('code');
		$session = strtolower($session);
		if($code != $session){
			$this->ajax(array('status'=>0,'message'=>'验证码不正确'));
		}else{
			$this->ajax(array('status'=>1,'message'=>'验证码正确'));
		}
		
	}
	
	
	
}
